package com.yjd.comm.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.yjd.comm.base.enums.DbWREnums;
import com.yjd.comm.system.model.MobileDataModel;
import com.yjd.comm.system.service.IMobileDataService;

public class MobileUtil {
	private static IMobileDataService mobileDataService;

	private MobileUtil() {
	}

	private static IMobileDataService getMobileService() {
		if (mobileDataService == null) {
			mobileDataService = ServiceHolder.getLocalService(
					IMobileDataService.class, "mobileDataService");
		}
		return mobileDataService;
	}

	public static String getMobileData(String mobile)
			throws NumberFormatException, Exception {
		String result = "";
		if (!mobile.equals("0") && mobile.length() > 7) {
			MobileDataModel mdm = getMobileService().getModelById(
					Long.valueOf(mobile.substring(0, 7)),DbWREnums.READ);
			if (mdm != null) {
				result = mdm.toString();
			}
		}
		return result;
	}

	/**
	 * 批量获取手机归属地
	 * 
	 * @param mobileList
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public static Map<Long, MobileDataModel> getMobileDataMap(
			List<Long> mobileList) throws Exception {
		if (mobileList == null || mobileList.size() == 0) {
			return null;
		}
		List<Long> mobiles = new ArrayList<Long>();
		Map<Long, Long> mbmap = new HashMap<Long, Long>();
		for (Long mobile : mobileList) {
			if (mobile != null && mobile.toString().length() > 7) {
				Long temp = Long.valueOf(mobile.toString().substring(0, 7));
				mobiles.add(temp);
				mbmap.put(mobile, temp);
			}
		}
		Map<Long, MobileDataModel> map = getMobileService().getModelMapByIds(
				mobiles,DbWREnums.READ);
		Map<Long, MobileDataModel> resultMap = new HashMap<Long, MobileDataModel>();
		for (Long mobile : mobileList) {
			Long temp = mbmap.get(mobile);
			if (temp != null) {
				resultMap.put(mobile, map.get(temp));
			}
		}
		return resultMap;
	}
}
